package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.CommunicationPkgCategoryAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.domain.inst.repository.SceneInstRepository;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.*;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@AttrHandler(CommunicationPkgCategoryAttrHandler.class)
public class CommunicationPkgCategoryAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //获取specID用来区分类别,是5G还是4G等。
        String specID = inst.getSpecId();
        //获取流量套包属性信息
        KiteAttr flowPackage = inst.getAttr("200004074");
        // 只有5G通用链接，4G通用链接，CAT1通用链接，NB通用链接，融合类CAT1通用连接产品有此联动
        if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equalsAny(specID,"40000017013","30000017015","30000017014","30000017013","30000017017","30000017019")){
            KiteOfferInst offerInst = querySceneInstService.getMainOfferInst(data.getSceneInstId());
            String offerId = offerInst.getOfferId();
            // 5G/4G纯数据的联动为 流量套包联动通信套包类型，逻辑见：FlowPackageAttrLinkHandler.java
            if(!KiteStringUtils.equalsAny(offerId,"50040701","50040702")) {
                //通信套包类型联动流量套包
                //选择月付单个连接、月付灵活共享时，流量套包默认月包，置灰；
                //选择预付单个连接时，流量套包可以选择季包、年包；
                List<KiteAttrValue> attrValues = flowPackage.getAttrValues();
                KiteAttrValue kiteAttrValueMonth = null;
                boolean hasJiBao = false;
                if (attrValues != null && attrValues.size() > 0) {
                    for (KiteAttrValue kiteAttrValue : attrValues) {
                        //匹配月包的枚举值value:100007194,将其去掉
                        if ("100007194".equals(kiteAttrValue.getAttrValue())) {
                            kiteAttrValueMonth = kiteAttrValue;
                        }
                        if ("100007195".equals(kiteAttrValue.getAttrValue())) {
                            hasJiBao = true;
                        }
                    }
                }
                if (KiteStringUtils.isNotBlank(attr.getValue()) && ("100006431".equals(attr.getValue()) || "100006432".equals(attr.getValue()))) {
                    //选择月付单个连接、月付灵活共享时，流量套包默认月包，置灰；
                    if (null == kiteAttrValueMonth) {
                        KiteAttrValue value = new KiteAttrValue();
                        value.setAttrValueId("201216509");
                        value.setAttrValueDesc("月包");
                        value.setAttrValue("100007194");
                        value.setAttrValueName("月包");
                        value.setAttrId(flowPackage.getAttrId());
                        attrValues.add(value);
                    }
                    flowPackage.setValue("100007194");
                    flowPackage.setIsEdit("F");
                } else {
                    //预付单个连接时候，不展示月包
                    if (null != kiteAttrValueMonth) {
                        attrValues.remove(kiteAttrValueMonth);
                    }
                    //如果有季包，则默认为季包，如果没有，则默认为年包
                    if (hasJiBao) {
                        flowPackage.setValue("100007195");
                        flowPackage.setValueDesc("季包");
                    } else {
                        flowPackage.setValue("100007196");
                        flowPackage.setValueDesc("年包");
                    }
                    flowPackage.setIsEdit("T");
                }
            }
            //流量套包的改变，再次联动套内用量
            //获取套内用量属性信息
            KiteAttr packageUsage = inst.getAttr("200004083");
            KiteAttr kiteAttr = new KiteAttr();
            kiteAttr.setAttrValueTypeId("98A");
            kiteAttr.setIsNull("F");
            if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equals(specID,"40000017013")){
                // 5G通用连接产品, 流量套包联动套内用量
                if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007194".equals(flowPackage.getValue())){
                    //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                    kiteAttr.setAttrId("202216546");//配置的月的
                    doSetAttrValues(kiteAttr,packageUsage,"100007222","100007222","30GB",inst,data);
                }else if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007195".equals(flowPackage.getValue())){
                    //如果流量套包选择的是季包，则套内用量联动展示月包的套内用量*3
                    kiteAttr.setAttrId("202106151436");//配置的季的
                    doSetAttrValues(kiteAttr,packageUsage,"110007706","110007706","90GB",inst,data);
                }else if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007196".equals(flowPackage.getValue())) {
                    //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                    kiteAttr.setAttrId("202106151446");//配置的年的
                    doSetAttrValues(kiteAttr, packageUsage, "110007724", "110007724", "360GB", inst, data);
                }
            }else if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equals(specID,"30000017015")){
                    // CAT1通用连接产品 ,流量套包联动套内用量
                    if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007194".equals(flowPackage.getValue())){
                        //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                        kiteAttr.setAttrId("202106151453");//配置的月的
                        doSetAttrValues(kiteAttr,packageUsage,"100007200","100007200","30MB",inst,data);
                    }else if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007196".equals(flowPackage.getValue())){
                        //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                        kiteAttr.setAttrId("202106151626");//配置的年的
                        doSetAttrValues(kiteAttr,packageUsage,"110007708","110007708","360MB",inst,data);
                    }
            }else if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equals(specID,"30000017014")){
                    //NB通用连接产品, 流量套包联动套内用量
                    if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007194".equals(flowPackage.getValue())){
                        //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                        kiteAttr.setAttrId("201218546");//配置的月的
                        doSetAttrValues(kiteAttr,packageUsage,"100007216","100007216","10MB",inst,data);
                    }else if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007196".equals(flowPackage.getValue())){
                        //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                        kiteAttr.setAttrId("202106151642");//配置的年的
                        doSetAttrValues(kiteAttr,packageUsage,"110007688","110007688","120MB",inst,data);
                    }
            }else if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equalsAny(specID,"30000017013", "30000017019")){
                    //4G通用连接产品 ,流量套包联动套内用量
                    if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007194".equals(flowPackage.getValue())){
                        //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                        kiteAttr.setAttrId("201216546");//配置的月的
                        doSetAttrValues(kiteAttr,packageUsage,"100007200","100007200","30MB",inst,data);
                    }else if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007195".equals(flowPackage.getValue())){
                        //如果流量套包选择的是季包，则套内用量联动展示月包的套内用量*3
                        kiteAttr.setAttrId("202106151651");//配置的季的
                        doSetAttrValues(kiteAttr,packageUsage,"110007687","110007687","90MB",inst,data);
                    }else if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007196".equals(flowPackage.getValue())){
                        //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                        kiteAttr.setAttrId("202106151713");//配置的年的
                        doSetAttrValues(kiteAttr,packageUsage,"110007708","110007708","360MB",inst,data);
                    }
            }else if(KiteStringUtils.isNotBlank(specID) && KiteStringUtils.equals(specID,"30000017017")){
                    //融合类CAT1通用连接产品,流量套包联动套内用量
                    if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007194".equals(flowPackage.getValue())){
                        //如果流量套包选择的是月包，则套内用量联动展示月包的套内用量，
                        kiteAttr.setAttrId("2021041440");//配置的月的
                        doSetAttrValues(kiteAttr,packageUsage,"100007200","100007200","30MB",inst,data);
                    }else if(KiteStringUtils.isNotBlank(flowPackage.getValue()) && "100007196".equals(flowPackage.getValue())){
                        //如果流量套包选择的是年包，则套内用量联动展示月包的套内用量*12
                        kiteAttr.setAttrId("202106151730");//配置的年的
                        doSetAttrValues(kiteAttr,packageUsage,"110007708","110007708","360MB",inst,data);
                    }
            }

        }

        if(KiteStringUtils.isNotBlank(specID) && (specID.startsWith("300000170") || specID.equals(LocalKeyConsts.FUNC_PROD_ID_YF5GZF_BASE))){
            // 针对资费计划基本信息功能产品
            KiteProdInst prodInst = querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(),inst.getInstId());
            if(KiteObjectUtils.isEmpty(prodInst)){
                return;
            }
            List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsBySceneInstId(prodInst.getProdInstId(),data.getSceneInstId());
            String basePriceFieldName = KiteStringUtils.equalsAny(attr.getValue(), "100006433", "100006434") ? "210005341" : "200002773";
            if(KiteListUtils.isNotEmpty(funcs)){
                for(KiteProdInstFunc func : funcs){
                    KiteAttr kiteAttr1 = func.getAttr(basePriceFieldName);
                    if (Objects.nonNull(kiteAttr1) && KiteStringUtils.isNotEmpty(kiteAttr1.getValue())) {
                        if ("200002773".equals(basePriceFieldName)) {
                            LocalSubscriptionFeeAttrLinkHandler bean = SpringUtil.getBean(LocalSubscriptionFeeAttrLinkHandler.class);
                            bean.handle(kiteAttr1, func, data);
                        }
                        else {
                            LocalSubscriptionFeeAttrLinkHandlerCopy bean = SpringUtil.getBean(LocalSubscriptionFeeAttrLinkHandlerCopy.class);
                            bean.handle(kiteAttr1, func, data);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        System.out.println();

    }

    private void doSetAttrValues(KiteAttr kiteAttr,KiteAttr packageUsage,String defaultValue,String value,String valueDesc,KiteInst inst, AttrData data){
        // 流量套包联动展示套内用量
        List<KiteAttrValue> need = SceneInstRepository.getKiteAttrValues(kiteAttr);
        packageUsage.setDefaultValue(defaultValue);
        packageUsage.setValue(value);
        packageUsage.setValueDesc(valueDesc);
        packageUsage.setAttrValues(need);
        // 流量套包联动展示套包价格
        if(!KiteStringUtils.isEmpty(packageUsage.getValue())){
            String code = DcPublicCache.getCodea("202012180001",packageUsage.getValue());
            if(!KiteStringUtils.isEmpty(code)){
                operateInstAttrService.setAttrValue(inst, "200003704", code, data.getSceneInstId(), null);
            }
        }
    }
}
